/**
 * Provides the FirstLight GameState example in the GQE library.
 *
 * @file examples/demo/GameState.cpp
 * @author Ryan Lindeman
 * @date 20110704 - Initial Release
 * @date 20110721 - Remove * from GetAsset() calls since it now returns TYPE&
 * @date 20110831 - Support new SFML2 snapshot changes
 */
#include "GameState.hpp"
#include <GQE/Core/assets/ImageAsset.hpp>
#include <GQE/Core/classes/App.hpp>
#include "Objects/ButtonObject.hpp"
#include "classes/Command.hpp"
#include "classes/ContentManager.hpp"
#include "Game_types.hpp"
GameState::GameState(GQE::App* theApp) :
    GQE::IState("Game",theApp),
    mMainConsole(32, 52),
    mLineIndex(0)
{
}

GameState::~GameState(void)
{
}

void GameState::DoInit(void)
{
    // First call our base class implementation
    IState::DoInit();

    // Check our App pointer
    assert(NULL != mApp && "GameState::DoInit() bad app pointer");
    sf::String anNewString;
    anNewString.SetFont(mApp->mAssetManager.GetFont("MainFont")->GetAsset());
    anNewString.SetText("");
    anNewString.SetColor(sf::Color::Black);
    anNewString.SetSize(16);
    gButtonWidth=gConfig->GetUint32("ButtonInfo","Width",0);
    gButtonHeight=gConfig->GetUint32("ButtonInfo","Height",0);

    unsigned int anLine;
    for(anLine=0; anLine<mConsoleConfig.GetUint32("Main","Lines",20); anLine++)
    {
        anNewString.SetPosition(2,320-16*anLine);
        mMainStrings.push_back(anNewString);
    }
    //Set up controls
    ButtonObject* newButton;
    newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("North");
	newButton->SetAction("Move North");
    newButton->SetPosition(128,320,0);
    newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("East");
	newButton->SetAction("Move East");
    newButton->SetPosition(256,352,0);
    newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("South");
	newButton->SetAction("Move South");
    newButton->SetPosition(128,384,0);
    newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("West");
	newButton->SetAction("Move West");
    newButton->SetPosition(32,352,0);
    newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("Look");
	newButton->SetAction("Look");
    newButton->SetPosition(32,320,0);
	gMainWindow=&mMainConsole;
	newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("Down");
	newButton->SetAction("Move Down");
    newButton->SetPosition(256,384,0);
	newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("Up");
	newButton->SetAction("Move Up");
    newButton->SetPosition(32,384,0);
	newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("Inventory");
	newButton->SetAction("Inventory");
    newButton->SetPosition(480,384,0);
	newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("Get");
	newButton->SetAction("Get");
    newButton->SetPosition(480,416,0);
	//Make map
    gMap=new MapClass();
    gMap->MakeDungeon();
	gPlayer->GiveItem(gContent->GetRandItem("Armor"),1);
	gPlayer->GiveItem(gContent->GetRandItem("Weapon"),1);
	gPlayer->GiveItem(gContent->GetRandItem("Weapon"),1);
    gPlayer->GiveItem(gContent->GetRandItem("Armor"),1);
	gPlayer->GiveItem(gContent->GetRandItem(),1);
	gPlayer->GiveItem(gContent->GetRandItem(),1);
	gPlayer->GiveItem(gContent->GetRandItem(),1);
	gPlayer->GiveItem(gContent->GetRandItem(),1);


    // Make sure our update loop is only called 30 times per second
    mApp->SetUpdateRate(30.0f);
	Commands::Look();
}

void GameState::ReInit(void)
{

}

void GameState::HandleEvents(sf::Event theEvent)
{
    if(theEvent.Type==sf::Event::KeyPressed)
    {
        if(theEvent.Key.Code==sf::Key::Num1)
        {
            gMap->PrintLevel(gPlayer->GetCoords().z);
        }
    }
}

void GameState::UpdateFixed(void)
{
    if(gMainWindow==NULL)
        gMainWindow=&mMainConsole;
    unsigned int anLine;
    unsigned int anLineCount=mConsoleConfig.GetUint32("Main","Lines",20);
    for(anLine=0; anLine<anLineCount; ++anLine)
    {
        mMainStrings[anLine].SetText(mMainConsole.GetLine(mMainConsole.mLineNum-anLine-1));
    }
    mObjectManager.UpdateFixed();
}

void GameState::UpdateVariable(float theElapsedTime)
{
    mObjectManager.UpdateVariable(theElapsedTime);
}

void GameState::Draw(void)
{
    mObjectManager.UpdateLists();
    mApp->mWindow.Clear(sf::Color(255,255,255));
    std::vector<sf::String>::iterator anIter;
    for(anIter=mMainStrings.begin(); anIter!=mMainStrings.end(); anIter++)
    {
        mApp->mWindow.Draw((*anIter));
    }
    mObjectManager.Draw();
}

void GameState::Cleanup(void)
{
    mObjectManager.ClearObjects();
    // Last of all, call our base class implementation
    IState::Cleanup();

}
